package com.example.uiclassassignments.Activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

import com.example.uiclassassignments.R;
import com.example.uiclassassignments.tools.SpinnerItemBean;

import java.util.ArrayList;

public class SpinnerActivity extends Activity {
    private ArrayList<SpinnerItemBean> mList4SpinnerItemBean;
    private ArrayList<Integer> mCheckedPos = new ArrayList<Integer>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spinner);
        Spinner spinner = findViewById(R.id.spinner4Car);
        SharedPreferences sp = getSharedPreferences("sp_1",MODE_PRIVATE);
        String strValue =  sp.getString("selCheckBox","");

        assert strValue != null;
        if(!strValue.equals("")) {
            String[] strAry = strValue.split(",");
            for (String item : strAry) {
            }
        }

        mList4SpinnerItemBean = new ArrayList<SpinnerItemBean>();
        mList4SpinnerItemBean.add(new SpinnerItemBean(R.drawable.ic_launcher_foreground,"Car1"));
        mList4SpinnerItemBean.add(new SpinnerItemBean(R.drawable.ic_launcher_foreground,"Car2"));
        mList4SpinnerItemBean.add(new SpinnerItemBean(R.drawable.ic_launcher_foreground,"Car3"));
        mList4SpinnerItemBean.add(new SpinnerItemBean(R.drawable.ic_launcher_foreground,"Car4"));

        SpinnerActivityItemAdapter adapter = new SpinnerActivityItemAdapter(this);
        spinner.setAdapter(adapter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        SharedPreferences sp = getSharedPreferences("sp_1",MODE_PRIVATE);
        SharedPreferences.Editor editor =  sp.edit();
        StringBuilder strValue = new StringBuilder();
        for (Integer inItem:mCheckedPos){
            if (inItem == null)
                continue;
            if (strValue.toString().equals(""))
                strValue = new StringBuilder(inItem + "");
            else {
                strValue.append(",").append(inItem);
            }
        }
        editor.putString("selCheckBox", strValue.toString());
        editor.apply();
    }

    public class SpinnerActivityItemAdapter extends BaseAdapter {
        private Context context;
        private LayoutInflater layoutInflater;

        SpinnerActivityItemAdapter(Context context) {
            this.context = context;
            this.layoutInflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return mList4SpinnerItemBean.size();
        }

        @Override
        public SpinnerItemBean getItem(int position) {
            return mList4SpinnerItemBean.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @SuppressLint("InflateParams")
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = layoutInflater.inflate(R.layout.spinner_item, null);
                convertView.setTag(new ViewHolder(convertView));
            }
            initializeViews(position,(SpinnerItemBean)getItem(position), (ViewHolder) convertView.getTag());
            return convertView;
        }

        private void initializeViews(final int position, SpinnerItemBean object, ViewHolder holder) {
            holder.ivCar.setImageResource(object.car_img);
            holder.tvCarBrand.setText(object.car_brand);
            if (mCheckedPos.contains(position))
                holder.checkbox4Item.setChecked(true);
            holder.checkbox4Item.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                    if (isChecked) {
                        if (!mCheckedPos.contains(position))
                            mCheckedPos.add(position);
                    }else {
                        mCheckedPos.remove(position);
                    }
                }
            });
        }

        class ViewHolder {
            private ImageView ivCar;
            private TextView tvCarBrand;
            private CheckBox checkbox4Item;

            ViewHolder(View view) {
                ivCar = (ImageView) view.findViewById(R.id.iv_car);
                tvCarBrand = (TextView) view.findViewById(R.id.tv_car_brand);
                checkbox4Item = (CheckBox)view.findViewById(R.id.cb);
            }
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }
}
